"""
    AUTHOR
        LiuBoYuan
    DESCRIPTION
        题库功能装饰器
        (装饰器模式的装饰器,非Python装饰器)
"""

import random


class Decorator:
    def __init__(self, Policy, Arg=None):
        """
            @param Policy: 要被装饰的策略对象
            @param Arg: 策略对象的参数
        """
        self.Policy = Policy
        self.Arg = Arg

    def LimitNumber(self, many):
        """
        @desc 限制出题数目
        @param many: 限制多少？
        @return: 限制后的列表
        """

        if self.Arg:
            myList = self.Policy.execute(self.Arg)
        else:
            myList = self.Policy.execute()

        if myList == []:
            print("空列表")
            return []

        randomList = []

        if many > len(myList):
            randomList = random.choices(myList, k=many)
            print(f"randomList{randomList}")
            while None in randomList:
                randomList = [
                    random.choice(randomList) if i is None else i
                    for i in randomList
                ]
            return randomList

        for i in range(many):
            while True:
                rand = random.randint(0, len(myList) - 1)
                if rand not in randomList:
                    if None == myList[rand]:
                        continue
                    randomList.append(rand)
                    break
        resultList = [myList[i] for i in randomList]
        return resultList
